<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">环保监测</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick">环保设备管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <div class="left-title flex">
          <el-button v-allow="'050101'" type="primary" @click="handleAdd()">添加环保设备</el-button>
        </div>
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-cascader v-model="treeId" placeholder="请选择考场组织" :options="schoolArray" :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'nextDatas',
              }" @change="changeTreeItem" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-input maxlength="50" show-word-limit placeholder="请输入设备名称" v-model="searchForm.name" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button v-allow="'050102'" type="primary" @click="onSubmit">查询</el-button>
            <el-button v-allow="'050102'" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="operation" slot-scope="scope">
          <span v-allow="'050103'" @click="handleParam(scope.row)" class="nx-btn nx-edit">参数</span>
          <span v-allow="'050103'" @click="handleEdit(scope.row)" class="nx-btn nx-edit">编辑</span>
          <span v-allow="'050104'" @click="handleDelete(scope.row)" class="nx-btn nx-del">删除</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog"></AddEditDialog>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import AddEditDialog from './components/addEditDialog.vue'

import { Page } from '@/utils/pagination'
import { getEnvironmentProtectionDeviceList, delEnvironmentProtectionDevice } from '@/api/environmentProtection'
import { getSchoolInfos } from '@/api/school'

export default {
  components: {
    NxTable,
    AddEditDialog
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        examinationRoomId: '',
        collegeId: '',
        facultyId: '',
        floorId: '',
        examinationRoomId: '',
        name: ''
      },
      // 表格字段
      tableCols: [
        {
          label: '考场名称',
          prop: 'examinationRoomName',
          align: 'center'
        },

        {
          label: '设备编码',
          prop: 'code',
          align: 'center'
        },
        {
          label: '设备名称',
          prop: 'name',
          align: 'center'
        },
        {
          label: '排序号',
          prop: 'orderNum',
          align: 'center'
        },
        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 180
        }
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      treeId: [], // 考场组织id集合
      schoolArray: [], // 考场组织集合
      userType: '', // 用户类型（0超级管理员，1考场管理员）
      examinationRoomId: '' // 考场id
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.examinationRoomId = Number(window.localStorage.getItem('examinationRoomId'))
    if (this.userType == 1) {
      this.searchForm.examinationRoomId = this.examinationRoomId
    }

    // 查询
    this.getList()

    // 获取考场组织
    this.getSchoolInfos()
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        examinationRoomId: '',
        collegeId: '',
        facultyId: '',
        floorId: '',
        examinationRoomId: '',
        name: ''
      }

      if (this.userType == 1) {
        this.searchForm.examinationRoomId = this.examinationRoomId
      }

      // 清空考场组织树
      this.treeId = []

      this.onSubmit()
    },
    // 查询列表
    getList() {
      getEnvironmentProtectionDeviceList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data
          this.pagination.count = res.count
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delEnvironmentProtectionDevice({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
      this.typeDialog = false
      this.energyConsumptionIsDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
      this.addEditIsDialog = false
      this.energyConsumptionIsDialog = false
    },
    // 获取考场组织
    getSchoolInfos() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场组织下拉切换
    changeTreeItem(val) {
      this.searchForm.examinationRoomId = this.userType == 1 ? this.examinationRoomId : ''
      this.searchForm.collegeId = ''
      this.searchForm.facultyId = ''
      this.searchForm.floorId = ''
      this.searchForm.examinationRoomId = ''

      if (val) {
        let len = val.length
        let id = val[len - 1]
        if (len == 1) {
          this.searchForm.examinationRoomId = id
        } else if (len == 2) {
          this.searchForm.collegeId = id
        } else if (len == 3) {
          this.searchForm.facultyId = id
        } else if (len == 4) {
          this.searchForm.floorId = id
        } else if (len == 5) {
          this.searchForm.examinationRoomId = id
        }
        this.searchForm.examinationRoomId = val[0]
      }

      // 查询
      this.getList()
    },
    // 监控视频
    handleMonitor(row) {
      this.$router.push({
        path: `/environmentProtection/environmentProtectionDeviceMonitor`,
        query: {
          environmentProtectionDeviceId: row.id,
          examinationRoomId: row.examinationRoomId
        }
      })
    },
    // 设备参数
    handleParam(row) {
      this.$router.push({
        path: `/environmentProtection/environmentProtectionDeviceParam`,
        query: {
          environmentProtectionDeviceId: row.id,
          examinationRoomId: row.examinationRoomId,
          deviceCode: row.code
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;
  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;
    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }
    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>